package model.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
//import org.apache.struts2.json.JSONException;
import dom.domain.Generaltype;
import dom.domain.GeneraltypeDAO;

public class GeneralTypeService {
	private GeneraltypeDAO gDao;
	private Map<String, String> map;

	public void setgDao(GeneraltypeDAO gDao) {
		this.gDao = gDao;
	}

	public String add(String name) {
		map = new HashMap<String, String>();
		if (name == null) {
			map.put("flag", "false");
			map.put("Info", "name为空");
			return JSONObject.fromObject(map).toString();
		}
		int count = gDao.getCount();
		Generaltype gType = new Generaltype();
		gType.setId(count + 1);
		gType.setName(name);
		map.put("flag", "true");
		map.put("Info", "添加成功");
		return JSONObject.fromObject(map).toString();
	}

	public String delete(int id) {
		map = new HashMap<String, String>();
		Generaltype gType = gDao.findById(id);
		if (gType != null) {
			gDao.delete(gType);
			gDao.updateId(id);
			map.put("flag", "true");
			map.put("Info", "delete成功");
		} else {
			map.put("flag", "false");
			map.put("Info", "id不存在");
		}
		return JSONObject.fromObject(map).toString();
	}

	public String getAll() {
		map = new HashMap<String, String>();
		List list = gDao.findAll();
		JSONArray array = new JSONArray();
		for (int i = 0; i < list.size(); i++) {
			Generaltype gType = (Generaltype) list.get(i);
			array.add(JSONObject.fromObject(gType.toString()));
		}
		if (array.size() > 0) {
			map.put("flag", "true");
			map.put("Info", "success");
			map.put("array", array.toString());
		} else {
			map.put("flag", "false");
			map.put("Info", "未添加类别");
		}
		return JSONObject.fromObject(map).toString();
	}
	public String modify(String Info)
	{
		map = new HashMap<String,String>();
		try
		{
			JSONObject jInfo = JSONObject.fromObject(Info);
			if(!jInfo.containsKey("id"))
			{
				map.put("flag", "false");
				map.put("Info", "id为空");
				return JSONObject.fromObject(map).toString();
			}
			if(jInfo.containsKey("name"))
			{
				map.put("flag", "false");
				map.put("Info", "name为空");
				return JSONObject.fromObject(map).toString();
			}
			Generaltype gType = gDao.findById(Integer.parseInt(jInfo.getString("id")));
			if(gType==null)
			{
				map.put("falg","false");
				map.put("Info", "id不存在");
				return JSONObject.fromObject(map).toString();
			}
			gType.setName(jInfo.getString("name"));
			gDao.attachDirty(gType);
			map.put("flag", "true");
			map.put("Info", "修改成功");
		}catch(JSONException e){
			map.put("flag","false");
			map.put("Info", e.getMessage());
		}
		return JSONObject.fromObject(map).toString();
	}
	public String query(int id)
	{
		Generaltype gType  = gDao.findById(id);
		if(gType!=null)
		{
			map.put("flag", "true");
			map.put("Info","查询成功");
			map.put("name", gType.getName());
		}
		else
		{
			map.put("flag", "false");
			map.put("Info","id不存在");
		}
		return JSONObject.fromObject(map).toString();
	}

}
